import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

// Modbus从设备配置数据结构定义
export interface ProtocolConfigField {
  /** 唯一标识 */
  id: number;

  /** 所属模板ID（外键关联template表） */
  templateId: number;

  /** 从机地址 */
  slaveAddress: number;

  /** 功能码 */
  functionCode: number;

  /** 寄存器起始地址 */
  registerAddress: number;

  /** 读取长度/寄存器数量 */
  readLength: number;

  /** 记录创建时间 */
  createdAt?: string | Date;

  /** 记录最后更新时间 */
  updatedAt?: string | Date;
}

export const createProtocolConfigFieldField = (
  tableAction: TableAction<ProtocolConfigField> = {}
): Fields[] => {
  return [
    {
      valueType: "number",
      inputType: "input",
      key: "id",
      title: "ID",
      disabled: true,
      dataIndex: "id",
      width: 80,
      isEdit: false,
      visible: true,
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "模板ID",
      dataIndex: "templateId",
      key: "templateId",
      placeholder: "请输入所属模板ID",
      rules: [
        { required: true, message: "模板ID不能为空" },
        { type: "number", min: 1, message: "模板ID必须大于0" },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "从机地址",
      dataIndex: "slaveAddress",
      key: "slaveAddress",
      placeholder: "请输入Modbus从站地址",
      rules: [
        { required: true, message: "从机地址不能为空" },
        {
          type: "number",
          min: 0,
          max: 255,
          message: "从机地址必须在0-255范围内",
        },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "功能码",
      dataIndex: "functionCode",
      key: "functionCode",
      placeholder: "请输入Modbus功能码",
      rules: [
        { required: true, message: "功能码不能为空" },
        { type: "number", min: 1, max: 4, message: "功能码必须在1-4范围内" },
      ],
      tooltip:
        "01-读取线圈, 02-读取离散输入, 03-读取保持寄存器, 04-读取输入寄存器",
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "寄存器起始地址",
      dataIndex: "registerAddress",
      key: "registerAddress",
      placeholder: "请输入寄存器起始地址",
      rules: [
        { required: true, message: "寄存器起始地址不能为空" },
        {
          type: "number",
          min: 0,
          max: 65535,
          message: "寄存器地址必须在0-65535范围内",
        },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "读取长度/寄存器数量",
      dataIndex: "readLength",
      key: "readLength",
      placeholder: "请输入读取长度/寄存器数量",
      rules: [
        { required: true, message: "读取长度不能为空" },
        {
          type: "number",
          min: 1,
          max: 125,
          message: "读取长度必须在1-125范围内",
        },
      ],
      tooltip: "Modbus协议限制一次最多读取125个寄存器",
    },
    {
      inputType: "date-picker",
      title: "创建时间",
      dataIndex: "createdAt",
      key: "createdAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      inputType: "date-picker",
      title: "更新时间",
      dataIndex: "updatedAt",
      key: "updatedAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      render: (_: any, record: ProtocolConfigField) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除此配置？"
            description="删除配置将无法恢复"
            onConfirm={() => tableAction.delete?.([record.id])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};
